//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FrameProviderOrderDlg.h"
#include "FormUtilities.h"
#include "Calendar.h"
#include "StringUtilities.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

const int CODE_DATE        = 1;
const int CODE_AMOUNT      = 2;
const int CODE_INVOICE_NO  = 3;

//---------------------------------------------------------------------------

__fastcall TfrmFrameProviderOrderDlg::TfrmFrameProviderOrderDlg(TComponent* Owner, TFrameProviderBridge* bridge, TFrameOrder* order)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->bridge = bridge;
   this->order  = order;
   lblCurrency->Caption = "(" + CurrencyString + ")";

   Caption = "Update order\\credit";
   original = new TFrameOrder( order);
   ShowOrder();
}

__fastcall TfrmFrameProviderOrderDlg::TfrmFrameProviderOrderDlg(TComponent* Owner, TFrameProviderBridge* bridge, __int64 frameProviderID)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->bridge  = bridge;
   this->frameProviderID = frameProviderID;
   this->order = NULL; 
   lblCurrency->Caption = "(" + CurrencyString + ")";

   Caption = "Add new order\\credit";
   original = NULL;
}

void TfrmFrameProviderOrderDlg :: ShowOrder()
{
   edtDate->Text   = DateToStr( order->orderDate);
   edtAmount->Text = FloatToStr( order->amount);
   edtNumber->Text = IntToStr( order->invoiceNumber);
}

void TfrmFrameProviderOrderDlg :: GatherOrder()
{
   order->orderDate     = StrToDate( edtDate->Text);
   order->amount        = edtAmount->Text;
   order->invoiceNumber = StrToInt( edtNumber->Text);
}

void TfrmFrameProviderOrderDlg :: AddNewOrder()
{
   order = new TFrameOrder();

   try
   {
      GatherOrder();
      order->frameProviderID = frameProviderID;
      bridge->AddOrder( order);
   }
   __finally
   {
      delete order;
      order = NULL;
   }//try-finally
}

void TfrmFrameProviderOrderDlg :: UpdateOrder()
{
   GatherOrder();
   bridge->UpdateOrder( original->id, order);
}

void TfrmFrameProviderOrderDlg :: ValidateData()
{
   if ( edtNumber->Text.Length() == 0)
      throw new TError( CODE_INVOICE_NO, "Invoice no. must be entered");

   try
   {
      StrToInt( edtNumber->Text);
   }
   catch( Exception * e)
   {
      throw new TError( CODE_INVOICE_NO, "Illegal invoice no.");
   }//try-catch

   if ( edtDate->Text.Length() == 0)
      throw new TError( CODE_DATE, "Date must be entered");

   try
   {
      TDateUtilities::validate( edtDate->Text, "Illegal date");
   }
   catch( Exception * e)
   {
      throw new TError( CODE_DATE, e->Message);
   }//try-catch
   
   if ( edtAmount->Text.Length() == 0)
      throw new TError( CODE_AMOUNT, "Amount must be entered");

   try
   {
      StrToFloat( edtAmount->Text);
   }
   catch( Exception * e)
   {
      throw new TError( CODE_AMOUNT, "Illegal amount");
   }//try-catch
}

void TfrmFrameProviderOrderDlg :: HandleError( TError * e)
{
   MessageDlg( e->Message,
               mtError,
               TMsgDlgButtons() << mbOK,
               0);

   switch ( e->GetCode())
   {
      case CODE_DATE :
         edtDate->SetFocus();
         break;

      case CODE_AMOUNT :
         edtAmount->SetFocus();
         break;

      case CODE_INVOICE_NO :
         edtNumber->SetFocus();
         break;
   }//switch

   ModalResult = mrNone;
}

//---------------------------------------------------------------------------

void __fastcall TfrmFrameProviderOrderDlg::btnOKClick(TObject *Sender)
{
   try
   {
      ValidateData();
      
      if ( order == NULL)
         AddNewOrder();
      else
         UpdateOrder();
   }
   catch( TError * e)
   {
      HandleError( e);
   }//try-catch
}
//---------------------------------------------------------------------------


void __fastcall TfrmFrameProviderOrderDlg::btnDateClick(TObject *Sender)
{
   TfrmCalendar * frmCalendar;

   frmCalendar = new TfrmCalendar( this);

   try
   {
      if ( edtDate->Text.Length() > 0)
         frmCalendar->calendar->Date = StrToDate( edtDate->Text);

      if ( frmCalendar->ShowModal() == mrOk)
         edtDate->Text = DateToStr( frmCalendar->calendar->Date);
   }
   __finally
   {
      delete frmCalendar;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmFrameProviderOrderDlg::FormDestroy(TObject *Sender)
{
   if ( original != NULL)
      delete original;
}
//---------------------------------------------------------------------------

